﻿using Newtonsoft.Json;
using SHDM.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http.Filters;

namespace SHDM.Web.Infrastructure
{
    public class ExceptionFilter : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            var responseMessage = new HttpResponseMessage();
            if (actionExecutedContext.Exception is NotImplementedException)
            {
                responseMessage.StatusCode = HttpStatusCode.NotImplemented;
            }

            NLog.LogManager.GetLogger("ExceptionFilter").Error(actionExecutedContext.Exception.ToString());
            string msg = JsonConvert.SerializeObject(new Result() { ErrorCode = 1, ErrorMessage = "异常请求:" + actionExecutedContext.Exception.Message });
            responseMessage.Content = new StringContent(msg, Encoding.UTF8, "application/json");
        }
    }
}